package com.david.viewtest.view

import android.content.Context
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.LinearGradient
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.Shader
import android.graphics.Typeface
import android.text.Layout
import android.text.StaticLayout
import android.text.TextPaint
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.drawable.toBitmap
import com.david.viewtest.R

class ShareBitmapCreateView(
    context: Context,
    attrs: AttributeSet?,
) : View(context, attrs) {
    val paint = TextPaint()


    val topBitmap: Bitmap = ResourcesCompat.getDrawable(resources, R.mipmap.canvas_img_top, null)!!.toBitmap()
    val bottomBitmap: Bitmap = ResourcesCompat.getDrawable(resources, R.mipmap.canvas_img_bottom, null)!!.toBitmap()

    var longImgBitmap: Bitmap =
        BitmapFactory.decodeStream(resources.assets.open("long_img3.png")) //ResourcesCompat.getDrawable(resources, R.drawable.long_img1, null)!!.toBitmap()


    val desWidth = (context.resources.displayMetrics.widthPixels + dp2px(16f))

    //真实图片宽度/设计宽度
    val realDesignWidthRatio = desWidth / 390f

    init {
        paint.textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 13f, resources.displayMetrics)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        super.setMeasuredDimension(desWidth.toInt(), ((longImgBitmap.height + (desWidth / topBitmap.width ) * topBitmap.height + (desWidth / bottomBitmap.width) * bottomBitmap.height).toInt()))
    }

    override fun onDraw(canvas: Canvas) {
        //super.onDraw(canvas)
        paint.color = Color.parseColor("#FF5841")

        canvas.drawRect(Rect(0, 0, width, height), paint)

        //绘制顶部默认图
        canvas.drawBitmap(
            topBitmap,
            Rect(0, 0, topBitmap.width, topBitmap.height),
            Rect(0, 0, desWidth.toInt(), ((desWidth/ topBitmap.width) * topBitmap.height).toInt()),
            paint
        )


        //绘制内容图
        canvas.drawBitmap(
            longImgBitmap,
            Rect(0, 0, longImgBitmap.width, longImgBitmap.height),
            Rect(dp2px(8f).toInt(), ((desWidth/ topBitmap.width) * topBitmap.height).toInt(), (desWidth - dp2px(8f)).toInt(), ((desWidth/ topBitmap.width) * topBitmap.height).toInt() + longImgBitmap.height),
            paint
        )


        //绘制底部默认图
        canvas.drawBitmap(bottomBitmap, Rect(0, 0, bottomBitmap.width, bottomBitmap.height), Rect(0, (height - (desWidth/ bottomBitmap.width) * bottomBitmap.height).toInt(), desWidth.toInt(), height), paint)
    }

    fun dp2px(dp: Float): Float {
        val r = Resources.getSystem()
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.displayMetrics)
    }
}